package com.qf.exam01.controller;

import ch.qos.logback.core.pattern.FormatInfo;
import com.qf.exam01.utils.IP;
import com.qf.exam01.utils.IPUtils;
import org.apache.tomcat.util.net.WriteBuffer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sound.sampled.Mixer;
import java.io.*;
import java.util.*;

@RestController
@RequestMapping("/ip")
public class IPContoller {
    @Value("${qfsy.ip.file01}")
    private String ipFile;

    @Value("${qfsy.ip.file02}")
    private String ipFile1;

    private List<String> ipList =new ArrayList<>();

    /**
     * 该接口生成指定数量的IP地址，并写入文本文件中
     * 文本文件的路径在yml文件中配置，key为 qfsy.ip.file01
     * @return
     */
    @GetMapping("/generate/{number}")
    public List<String> generateIP(@PathVariable("number") Integer number) throws Exception {
        List<String> ips=new ArrayList<>();
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(ipFile));
        //通过num生成IP个数
        for (int i = 0; i < number; i++) {
            String ip = IPUtils.randomIP().toString();
            ips.add(ip);
            bufferedWriter.write(ip);
            bufferedWriter.newLine();
        }
        bufferedWriter.flush();
        bufferedWriter.close();
        return ips;
    }


    /**
     * 查询上次生成的随机IP地址
     * @return
     */
    @GetMapping("/query")
    public List<String> queryLast() throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(ipFile));
        ArrayList<String> list = new ArrayList<>();
        String s="";
        while ((s=bufferedReader.readLine())!=null){
            list.add(s);

        }
        bufferedReader.close();
        return list;
    }


    /**
     * IP地址去重和统计
     * 1. 把不重复的IP地址写入一个新的文本文件中，文件配置在yml中，key为 qfsy.ip.file01
     * 2. 并在接口中返回
     * - 不重复的IP
     * - IP出现的次数
     * - 重复次数最多的5个IP
     * @return
     */
    @GetMapping("/s")
    public HashMap statistic() throws Exception {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(ipFile));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(ipFile1));
        HashMap<String, Object> all = new HashMap<>();
        HashMap<String, Integer> map = new HashMap<>();
        HashMap<String, Integer> top5 = new HashMap<>();
        String line;
        while ((line=bufferedReader.readLine())!=null){
            if (map.containsKey(line)){
                map.put(line,map.get(line)+1);
            }else{
                map.put(line,1);
                //把不重复的ip写入文件
                bufferedWriter.write(line+"\n");
            }
        }
        int val=0;
        String key="";
        for (int i = 0; i < 5; i++) {
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                if (val < entry.getValue()) {
                    key = entry.getKey();
                    val = entry.getValue();
                }
            }
            top5.put(key,val);
            map.remove(key);
            val=0;
        }

        all.put("去重后的IP：",map);
        all.put("前五的数据：",top5);
        bufferedWriter.flush();
        bufferedWriter.close();
        bufferedReader.close();
        return all;
    }


}
